<?php
/**
 * *********************************************************************************************************************
 * 国通敏思达接口：验证单号是否做过派件
 * *********************************************************************************************************************
 */
namespace soa\partner\gt;

use artisan\http;

class WaybillSendCheck
{
    protected $service_url = 'http://101.227.81.102:22229/gto-third-ifd-web/pda/waybillSentCheck!checkWaybillSent.action';

    const APP_KEY = 'MrXdhruG2NAIaCqE';

    /**
     * @param $waybillNo
     * @param $branchCode
     * @return array|mixed
     */
    public function checkDelivery($waybillNo, $branchCode)
    {
        $time1 = time();

        $data = [
            'branchCode' => $branchCode,
            'billCode'   => $waybillNo,
        ];
        $result = $this->requestApi($data);

        if(empty($result)){
            logDebug($data, 'gt_send_check_no_result1', time() - $time1);
        }else {
            $result = json_decode($result, true);

            if (json_last_error() == 0) {
                if(isset($result['resultCode']) && !in_array($result['resultCode'], ['1000'])){
                    logDebug('获取国通是否签收信息返回错误:请求参数:' . json_encode($data, JSON_UNESCAPED_UNICODE).' 返回值:'.$result, 'gt_send_check_no_result2',(time()-$time1));
                }
                return $result;
            }else{
                logDebug('获取国通是否签收信息请求超时:请求参数:' . json_encode($data, JSON_UNESCAPED_UNICODE).' 返回值:'.$result, 'gt_send_check_no_result3',(time()-$time1));
            }
        }
        return [];
    }

    /**
     * @param $data
     * @return mixed
     */
    public function requestApi($data)
    {
        $param = mb_convert_encoding(json_encode($data), 'UTF-8');

        $post = [
            'params'   => $param,
            'digest'   => base64_encode(md5($param . self::APP_KEY)),
            'fileName' => '',
            'format'   => 'JSON',
            'identify' => 'KB',
        ];
        
        $option = [
            'TIMEOUT' => 5,
        ];

        return http::post($this->service_url, $post, $option);
    }
}